// Fill out your copyright notice in the Description page of Project Settings.


#include "AIController/EnemyAIController.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "Character/EnemyCharacter.h"
#include "Helper/DebugHelper.h"
#include "Kismet/GameplayStatics.h"

AEnemyAIController::AEnemyAIController()
{
	PrimaryActorTick.bCanEverTick = true;
}

void AEnemyAIController::BeginPlay()
{
	Super::BeginPlay();
	//Debug::printLog("enemyAi on beginPlay");
	/*// 控制enemy移动至玩家0
	APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(),0);
	if(PlayerPawn)
	{
		// ai操控enemy聚焦玩家0
		SetFocus(PlayerPawn);
		// ai操控玩家移动至玩家0
		MoveToActor(PlayerPawn,150);
	}*/


	/*// 行为树方式
	APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(),0);
	if(PlayerPawn)
	{
		// ai操控执行enemy的行为树
		AEnemyCharacter* Enemy = Cast<AEnemyCharacter>(GetPawn());
		RunBehaviorTree(Enemy->BehaviorTree);
		// 设置行为树黑板里的参数
		UBlackboardComponent* BlackboardComponent = GetBlackboardComponent();
		BlackboardComponent->SetValueAsObject(FName("PlayerPawn"),PlayerPawn); 
	}*/
	// 如果放到beginplay去开始控制enemy 在spwan的情况下会拿不到enemy 所以要用onpeccess方法
	// AEnemyCharacter* enemy = Cast<AEnemyCharacter>(GetPawn());
	// if (enemy)
	// {
	// 	RunBehaviorTree(enemy->BehaviorTree);
	// }
}

void AEnemyAIController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
}

void AEnemyAIController::OnPossess(APawn* InPawn)
{
	//Debug::printLog("enemyAi on possess");
	Super::OnPossess(InPawn);
	AEnemyCharacter* enemy = Cast<AEnemyCharacter>(InPawn);
	if (enemy)
	{
		RunBehaviorTree(enemy->BehaviorTree);
	}
}

void AEnemyAIController::SetAIDamage()
{
	// 设置行为树黑板里的参数
	UBlackboardComponent* BlackboardComponent = GetBlackboardComponent();
	APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0);
	if (BlackboardComponent)
	{
		if (PlayerPawn)
		{
			//Debug::printLog("set blackboard playerPawn");
			BlackboardComponent->SetValueAsObject(FName("PlayerPawn"), PlayerPawn);
		}
	}
	// Debug::printLog("ai controller do damage");
	// UBlackboardComponent* BlackboardComponent = GetBlackboardComponent();
	BlackboardComponent->SetValueAsBool(FName("IsDamage"), true);
}
